const md5 = require('md5')
const executeSql = require('../utils/query.js')
const {PASSWORD_SALT} = require('../config')


exports.updatePasswordById = async (admin_id, oldPassword, newPassword) => {
    let sql = `update manager set password = '${newPassword}' where admin_id = ${admin_id} and password = '${oldPassword}';`
    return await executeSql(sql)
}


exports.getUserPasswordAndId = async (admin_id, oldPassword) => {
    let sql = `select * from manager where password = '${oldPassword}' and admin_id = ${admin_id};`
    return await executeSql(sql)
}


exports.updateAvatar = async (admin_id, avatar) => {
    let sql = `update manager set avatar = '${avatar}' where admin_id = ${admin_id}`
    return await executeSql(sql)
}


exports.getUserInfo = async (admin_id) => {// 1. 查数据库
    let sql = `select admin_id,username,avatar,nickname,add_date,last_login_date from manager where admin_id = ${admin_id}`
    return await executeSql(sql)
}

exports.Login = async (username, password) => {
    password = md5(password + PASSWORD_SALT)
    // 操作数据库 ,编写sql,真实业务中，肯定要去数据库中匹配
    let sql = `select * from manager where username='${username}' and password = '${password}' limit 1`
    return await executeSql(sql)
}

exports.getUserInfoById = async (admin_id) => {
    // 操作数据库 ,编写sql,真实业务中，肯定要去数据库中匹配
    let sql = `select * from manager where admin_id = ${admin_id} limit 1`
    return await executeSql(sql)
}

exports.getAdminList = async () => {
    // 操作数据库 ,编写sql,真实业务中，肯定要去数据库中匹配
    let sql = `select admin_id,username,nickname from manager`
    return await executeSql(sql)
}